//
//  LLGVisitorView.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/8.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit

class LLGVisitorView: UIView {
    
    var loginClosure: (() -> ())?

    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩图片
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //中心图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //信息
    private lazy var MessageLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //居中
        label.textAlignment = .Center
        //自动换行
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        //灰色
        label.textColor = UIColor.darkGrayColor()

        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: --设置界面
    private func setupUI() {
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    
        //添加视图
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(MessageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        MessageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(MessageLabel.snp_bottom).offset(10)
            make.leading.equalTo(MessageLabel)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(MessageLabel)
            make.size.equalTo(registerButton)
        }
        
        startAnimation()
      
    }
    
    // MARK: --旋转动画
    private func startAnimation() {
    
        //创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置时长为20
        animation.duration = 20
        //动画目的地
        animation.toValue = 2 * M_PI
        
        //动画次数
        animation.repeatCount = MAXFLOAT
        
        //不释放动画
        animation.removedOnCompletion = false
        
        //添加动画
        cycleImageView.layer.addAnimation(animation, forKey: "animation")
    }
    
    // MARK: --设置不同控制器下,显示内容
    func updataVistiorViewInfo(message: String?, imageName: String?) {
    
        if let msg = message, imgName = imageName {
        
            MessageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            
            cycleImageView.hidden = true
        } else {
        
            cycleImageView.hidden = false
        }
    }
    
    @objc private func registerAction() {
    
        print("注册")
        
        loginClosure?()
    }
    
    @objc private func loginAction() {
        
        print("登录")
        loginClosure?()
    }
}
